package com.cheng.dyjk.mon;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cheng.dyjk.mon.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*@Controller*/
@Component //把这个类归spring容器管理
public class SpringRestTemplateController {

    @Resource
    private ItemService itemService;

   /* @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/testGetApi")
    public String getJson() {
        String url = "http://localhost:8081/getListUserALL";
        //String json =restTemplate.getForObject(url,Object.class);
        ResponseEntity<String> results = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
        System.out.println(results);
        String json = results.getBody();

        return json;
    }*/

    @PostConstruct  //这个方法在这个类被创建时调用，理解为构造器里的方法
    public void getApi() {
        // 这个ItemService是自己new的，不是spring管理的，所以无法进行依赖注入，里面的属性是null
        // 你应该把它变成一个bean
//        ItemService itemService = new ItemService();

        //在获取json数据前先将表清空
        Item item1 =new Item();
        itemService.truncate(item1);


        RestTemplate restTemplate = new RestTemplate();
        //请求接口的地址
        String url = "http://127.0.0.1:8081/getListUserALL";
        //String json =restTemplate.getForObject(url,Object.class);
        //springboot RestTemplate类帮我们写好了 请求这个地址，打开这个地址等
        ResponseEntity<String> results = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
        //返回json字符串
        String resultJson = results.getBody();
        //将json字符串封装为json对象
        JSONObject jsonObject = JSON.parseObject(resultJson);
        System.out.println(jsonObject);
//        JSONObject resultJsonObject  = jsonObject.getJSONObject("data");
        JSONArray arrayData = jsonObject.getJSONArray("data");
//        System.out.println(arrayData);

        List<Item> items = JSONArray.parseArray(arrayData.toJSONString(), Item.class);
//        System.out.println(items);

        //循环遍历数组
        for (Item item : items) {
            //TODO 插入数据库
            itemService.save(item);
            System.out.println(item);
        }


//        JSONObject dataArray = resultJsonObject.getJSONObject("data");
//        System.out.println(dataArray);
//        List<Item> items = JSONArray.parseArray(arrayData.toJSONString(), Item.class);
//        System.out.println(items);
        System.out.println("----");
        System.out.println("=====");
        System.out.println("master test");
        System.out.println("hot-fix test");

    }
}
